<?php
class UI
{
    function showGoods($goods_data) {
        # 以表格的形式打印
        echo '货架：';
        echo '<table border=1>';
        echo '<tr><th>ID</th><th>名字</th><th>单价</th><th>数量</th><th>操作</th></tr>';
        foreach ($goods_data as $row) {
            echo "<tr>
            <th>{$row['id']}</th>
            <th>{$row['name']}</th>
            <th>{$row['salary']}</th>
            <th>{$row['number']}</th>
            <th>
                <a href='cart/join.php?name={$row["name"]}'>添加</a>
            </th>
            </tr>";
        }
        echo '</table>';
    }
    // 显示购物车
    function showCart(array $data_array) 
    {
        $total_money = 0;
        $total_count = 0;
        # 以表格的形式打印
        echo '购物车：';
        echo '<table border=1>';
        echo '<tr><th>ID</th><th>名字</th><th>数量</th><th>单价</th><th>总价</th><th colspan = "2">操作</th></tr>';
        if (!empty($data_array)) {
            $i = 0; 
            foreach ($data_array as $row) {
                $cart_name = $row['name'];
                $cart_count = $row['count'];
                $goods_salary = $row['salary'];
                $total = $row['total'];
                $total_money += $total;
                $total_count += $cart_count;
                echo "<tr>
                <th>{$i}</th>
                <th>{$cart_name}</th>
                <th>{$cart_count}</th>
                <th>{$goods_salary}</th>
                <th>{$total}</th>
                <th>
                    <a href='cart/dec.php?name={$cart_name}'>减少</a>
                </th>
                <th>
                    <a href='cart/inc.php?name={$cart_name}'>增加</a>
                </th>
                </tr>";
                $i += 1;                
            }    
        }
        echo "<tr>
            <th>总计</th>
            <th></th>
            <th>{$total_count}</th>
            <th></th>
            <th>{$total_money}</th>
            <th></th>
            <th></th>
            </tr>";
        echo '</table>';        
    }

    function showUser(string $name, int $money) 
    {
        echo "<p>账户：{$name} 余额：{$money}</p>";
    }
    function addLink($url, $text) {
        echo "<a href={$url}>{$text}</a>";
    }
    function addLinkNextLine($url, $text) {
        echo "<br/><a href={$url}>{$text}</a>";
    }
}


?>